#include "board.h"

#include <cstring>

Board::Board() {
    memset(map, 0, sizeof(map));  // 初始化为0
}

int Board::getStatus(int x, int y) {
    return map[x][y];
}

void Board::setStatus(int x, int y, int _status) {
    map[x][y] = _status;
}

void Board::revert() {
    for (auto& i : map) {
        for (int& j : i) {
            j *= -1;
        }
    }
};

// 判断有问题
int Board::rule(int x, int y) {
    int count[4] = {0, 0, 0, 0};
    int value = map[x][y];
    if (map[x][y] != 0) {
        for (int i = 1; i < 5; i++) {
            //竖排
            if (x + i < 15 && map[x + i][y] == value)
                count[0] += 1;
            if (x - i >= 0 && map[x - i][y] == value)
                count[0] += 1;
            //横排
            if (y + i < 15 && map[x][y + i] == value)
                count[1] += 1;
            if (y - i >= 0 && map[x][y - i] == value)
                count[1] += 1;
            //斜排
            if (x + i < 15 && y - i >= 0 && map[x + i][y - i] == value)
                count[2] += 1;
            if (x - i >= 0 && y + i < 15 && map[x - i][y + i] == value)
                count[2] += 1;
            if (x + i < 15 && y + i < 15 && map[x + i][y + i] == value)
                count[3] += 1;
            if (x - i >= 0 && y - i >= 0 && map[x - i][y - i] == value)
                count[3] += 1;
        }
        for (int i : count)
            if (i >= 4)
                return value;  // 胜利
        return 0;
    } else
        return 0;
}
